package com.twu.biblioteca;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Vector;


/**
 * Created with IntelliJ IDEA.
 * User: Deepika
 * Date: 5/13/13
 * Time: 6:53 PM
 * To change this template use File | Settings | File Templates.
 */

import static javax.swing.JOptionPane.showInputDialog;
import static javax.swing.JOptionPane.showMessageDialog;


public class Biblioteca{

    private JPanel panel1;
    private JToolBar ToolBar;
    private JButton viewBooksInLibraryButton;
    private JButton reserveABookButton;

    private JButton reserveButton;
    private JButton membershipDetailsButton;

    BibliotecaLibrary biblioteca = new BibliotecaLibrary();


    JTable table1;


    public void bibliotecaActionListeners(){

    viewBooksInLibraryButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            viewBooksButtonClick();
        }
    });

    reserveABookButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                reserveBookButtonClick();
            }
        });

    membershipDetailsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                membershipButtonClick();
            }
        });

    reserveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                reserveButtonClick();
            }
        });

    }

    public void addBooksToTable() {

        DefaultTableModel tableModel = new DefaultTableModel(0,0);
        ArrayList<Book> books = new ArrayList<Book>();
        String header[] = new String[]{"Books"};
        tableModel.setColumnIdentifiers(header);
        table1.setModel(tableModel);

        books = biblioteca.getAllBooks();

        for(Book b: books){
            Vector v = new Vector();
            v.add(b.getName());
            tableModel.addRow(v);
        }

        table1.setVisible(true);
    }

    public void viewBooksButtonClick(){
        addBooksToTable();
        table1.setEnabled(false);
        reserveButton.setVisible(false);
    }

    public void reserveBookButtonClick(){
        addBooksToTable();
        table1.setEnabled(true);
        reserveButton.setVisible(true);
    }

    public void membershipButtonClick(){
            showMessageDialog(null, "Please talk to a Librarian. Thank you.");
    }

    public void reserveButtonClick(){
        Integer index = table1.getSelectedRowCount();
        biblioteca.reserveBooks(index);
    }



    public Biblioteca() {

        //do not display the reserve button initially
        reserveButton.setVisible(false);
        table1.setVisible(false);

         bibliotecaActionListeners();
    }


    public static void main(String[] args) {

        JFrame frame = new JFrame("Welcome to Biblioteca");
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.setLocation(300,300);
        frame.setContentPane(new Biblioteca().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.isResizable();
        frame.pack();
        showMessageDialog(null, "Welcome to Biblioteca!");

    }

}
